/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package TimeRecording.tables;

import TimeRecording.business.DateRange;
import TimeRecording.enums.HolidayAdjustmentStatus;
import TimeRecording.enums.HolidayGrantType;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Query;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

/**
 *
 * @author david
 */
@Entity
@Table(name = "holiday_adjustment")
@NamedQueries({
    @NamedQuery(
            name = "HolidayAdjustment.findAll",
            query = "SELECT h " +
                    "FROM HolidayAdjustment h"),
    @NamedQuery(
            name = "HolidayAdjustment.findByAdjustmentId",
            query = "SELECT h " +
                    "FROM HolidayAdjustment h " +
                    "WHERE h.adjustmentId = :adjustmentId"),
    @NamedQuery(
            name = "HolidayAdjustment.findByAdjustmentType",
            query = "SELECT h " +
                    "FROM HolidayAdjustment h " +
                    "WHERE h.adjustmentType = :adjustmentType"),
    @NamedQuery(
            name = "HolidayAdjustment.findByDateRange",
            query = "SELECT h " +
                    "FROM HolidayAdjustment h " +
                    "WHERE h.adjustmentDate BETWEEN :lowerAdjustmentDate AND " +
                                                    ":upperAdjustmentDate"),
    @NamedQuery(
            name = "HolidayAdjustment.findByEmployeeAndDate",
            query = "SELECT h " +
                    "FROM HolidayAdjustment h " +
                    "WHERE h.employeeId = :employeeId AND " +
                         "h.adjustmentDate = :adjustmentDate"),
    @NamedQuery(
            name = "HolidayAdjustment.findByEmployeeDateAndType",
            query = "SELECT h " +
                    "FROM HolidayAdjustment h " +
                    "WHERE h.employeeId = :employeeId AND " +
                         "h.adjustmentDate = :adjustmentDate AND " +
                         "h.adjustmentType = :adjustmentType"),
    @NamedQuery(
            name = "HolidayAdjustment.findByEmployeeAndDateRange",
            query = "SELECT h " +
                    "FROM HolidayAdjustment h " +
                    "WHERE h.employeeId = :employeeId AND " +
                         "(h.adjustmentDate BETWEEN :lowerAdjustmentDate AND " +
                                                    ":upperAdjustmentDate)")})

public class HolidayAdjustment implements Serializable {

    public final static String BASE_QUERY = "SELECT h FROM HolidayAdjustment h";

    public final static String EMPLOYEE_FILTER = "h.employeeId = :employeeId";
    public final static String ADJUSTMENT_TYPE_FILTER = "h.adjustmentType = :adjustmentType";
    public final static String DATE_FILTER = "(h.adjustmentDate BETWEEN :lowerAdjustmentDate AND :upperAdjustmentDate)";

    public final static String COL_EMPLOYEE_ID = "employeeId";
    public final static String COL_ADJUSTMENT_TYPE = "adjustmentType";
    public final static String COL_DATE = "adjustmentDate";
    public final static String COL_LOWER_DATE = "lowerAdjustmentDate";
    public final static String COL_UPPER_DATE = "upperAdjustmentDate";

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "adjustment_id")
    private Integer adjustmentId;

    @Column(name = "hours")
    private double hoursAdjusted;

    @Column(name = "type")
    private HolidayGrantType adjustmentType;

    @Column(name = "administrator_comment")
    private String comment;

    @Column(name = "status")
    private HolidayAdjustmentStatus status;

    @Column(name = "date")
    @Temporal(TemporalType.DATE)
    private Date adjustmentDate;

    @JoinColumn(name = "administrator_id", referencedColumnName = "employee_id")
    @ManyToOne
    private Employee administratorId;

    @JoinColumn(name = "employee_id", referencedColumnName = "employee_id")
    @ManyToOne(optional = false)
    private Employee employeeId;

    // <editor-fold defaultstate="collapsed" desc="Property Change Support">
    @Transient
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);

    public void addPropertyChangeListener(PropertyChangeListener listener) {
	changeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
	changeSupport.removePropertyChangeListener(listener);
    } // </editor-fold>

    public HolidayAdjustment() {
        adjustmentType = HolidayGrantType.ANNUAL;
        status = HolidayAdjustmentStatus.PREPARATION;
    }

    public HolidayAdjustment(Integer adjustmentId) {
        this();
        this.adjustmentId = adjustmentId;
    }

    public Integer getAdjustmentId() {
        return adjustmentId;
    }

    public void setAdjustmentId(Integer adjustmentId) {
        Integer oldAdjustmentId = this.adjustmentId;
        this.adjustmentId = adjustmentId;
        changeSupport.firePropertyChange("adjustmentId", oldAdjustmentId, adjustmentId);
    }

    public double getHoursAdjusted() {
        return hoursAdjusted;
    }

    public void setHoursAdjusted(double hoursAdjusted) {
        double oldHoursAdjusted = this.hoursAdjusted;
        this.hoursAdjusted = hoursAdjusted;
        changeSupport.firePropertyChange("hoursAdjusted", oldHoursAdjusted, hoursAdjusted);
    }

    public HolidayGrantType getAdjustmentType() {
        return adjustmentType;
    }

    public void setAdjustmentType(HolidayGrantType adjustmentType) {
        HolidayGrantType oldAdjustmentType = this.adjustmentType;
        this.adjustmentType = adjustmentType;
        changeSupport.firePropertyChange("adjustmentType", oldAdjustmentType, adjustmentType);
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        String oldComment = this.comment;
        this.comment = comment;
        changeSupport.firePropertyChange("comment", oldComment, comment);
    }

    public HolidayAdjustmentStatus getStatus() {
        return status;
    }

    public void setStatus(HolidayAdjustmentStatus status) {
        HolidayAdjustmentStatus oldStatus = this.status;
        this.status = status;
        changeSupport.firePropertyChange("status", oldStatus, status);
    }

    public Date getAdjustmentDate() {
        return adjustmentDate;
    }

    public void setAdjustmentDate(Date adjustmentDate) {
        Date oldAdjustmentDate = this.adjustmentDate;
        this.adjustmentDate = adjustmentDate;
        changeSupport.firePropertyChange("adjustmentDate", oldAdjustmentDate, adjustmentDate);
    }

    public Employee getAdministratorId() {
        return administratorId;
    }

    public void setAdministratorId(Employee administratorId) {
        Employee oldAdministratorId = this.administratorId;
        this.administratorId = administratorId;
        changeSupport.firePropertyChange("administratorId", oldAdministratorId, administratorId);
    }

    public Employee getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(Employee employeeId) {
        Employee oldEmployeeId = this.employeeId;
        this.employeeId = employeeId;
        changeSupport.firePropertyChange("employeeId", oldEmployeeId, employeeId);
    }

    public boolean isApproved() {
        return status != HolidayAdjustmentStatus.PREPARATION;
    }
    
    public static Query getHolidayAdjustmentsForEmployeeAndDateRange(
            EntityManager entityManager, Employee employee, DateRange dateRange) {

        return entityManager.createNamedQuery(
                "HolidayAdjustment.findByEmployeeAndDateRange").
                setParameter(COL_EMPLOYEE_ID, employee).
                setParameter(COL_LOWER_DATE, dateRange.getFromDate()).
                setParameter(COL_UPPER_DATE, dateRange.getToDate());
    }

    public static List<HolidayAdjustment> getHolidayAdjustmentsForEmployeeDateAndType(
            EntityManager entityManager, Employee employee, Date date,
            HolidayGrantType type) {

        return entityManager.createNamedQuery(
                "HolidayAdjustment.findByEmployeeDateAndType").
                setParameter(COL_EMPLOYEE_ID, employee).
                setParameter(COL_DATE, date).
                setParameter(COL_ADJUSTMENT_TYPE, type).getResultList();
    }

    public static Query getHolidayAdjustmentsForDateRange(
            EntityManager entityManager, DateRange dateRange) {

        return entityManager.createNamedQuery(
                "HolidayAdjustment.findByDateRange").
                setParameter(COL_LOWER_DATE, dateRange.getFromDate()).
                setParameter(COL_UPPER_DATE, dateRange.getToDate());
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (adjustmentId != null ? adjustmentId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof HolidayAdjustment)) {
            return false;
        }
        HolidayAdjustment other = (HolidayAdjustment) object;
        if ((this.adjustmentId == null && other.adjustmentId != null) || (this.adjustmentId != null && !this.adjustmentId.equals(other.adjustmentId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "TimeRecording.tables.HolidayAdjustment[adjustmentId=" + adjustmentId + "]";
    }

    public void copy(HolidayAdjustment holidayAdjustment) {

        this.setAdjustmentId(holidayAdjustment.getAdjustmentId());
        this.setHoursAdjusted(holidayAdjustment.getHoursAdjusted());
        this.setAdjustmentType(holidayAdjustment.getAdjustmentType());
        this.setComment(holidayAdjustment.getComment());
        this.setStatus(holidayAdjustment.getStatus());
        this.setAdjustmentDate(holidayAdjustment.getAdjustmentDate());
        this.setEmployeeId(holidayAdjustment.getEmployeeId());
        this.setAdministratorId(holidayAdjustment.getAdministratorId());
    }
}
